<?php /* Smarty version Smarty-3.1.8, created on 2012-11-05 23:00:58
         compiled from "D:\personalweb\personal\tomatoframework\tomatocli/template\dao.class.tpl" */ ?>
<?php /*%%SmartyHeaderCode:154855097d4aaecaa49-79568409%%*/if(!defined('SMARTY_DIR')) exit('no direct access allowed');
$_valid = $_smarty_tpl->decodeProperties(array (
  'file_dependency' => 
  array (
    '359d807202d6a2c372377c11e5513a6b9f645716' => 
    array (
      0 => 'D:\\personalweb\\personal\\tomatoframework\\tomatocli/template\\dao.class.tpl',
      1 => 1352125834,
      2 => 'file',
    ),
  ),
  'nocache_hash' => '154855097d4aaecaa49-79568409',
  'function' => 
  array (
  ),
  'variables' => 
  array (
    'resourceName' => 0,
    'pk' => 0,
    's_modifyFields' => 0,
    's_showFields' => 0,
    's_listFields' => 0,
    'col' => 0,
    'k' => 0,
    'v' => 0,
    'caption' => 0,
    'tableName' => 0,
  ),
  'has_nocache_code' => false,
  'version' => 'Smarty-3.1.8',
  'unifunc' => 'content_5097d4aaf36dd4_34088479',
),false); /*/%%SmartyHeaderCode%%*/?>
<?php if ($_valid && !is_callable('content_5097d4aaf36dd4_34088479')) {function content_5097d4aaf36dd4_34088479($_smarty_tpl) {?><<?php ?>?php

/*
 * Tomato Framework
 * 
 * Copyright 2012-2012 Luax.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * @link http://www.tomatoframework.com
 * @link http://www.luaxlou.com
 * @version $Id$
 */

/**
 * 
 * 
 * @package tomatoapp
 * @author Luax <luaxcn@gmail.com>
 */
class <?php echo ucfirst($_smarty_tpl->tpl_vars['resourceName']->value);?>
Dao {

    public static $pk = '<?php echo $_smarty_tpl->tpl_vars['pk']->value;?>
';

    public static $modifyfields = '<?php echo $_smarty_tpl->tpl_vars['s_modifyFields']->value;?>
';
    public static $showfields = '<?php echo $_smarty_tpl->tpl_vars['s_showFields']->value;?>
';
    public static $listfields = '<?php echo $_smarty_tpl->tpl_vars['s_listFields']->value;?>
';

    public static $columns = array(
        <?php  $_smarty_tpl->tpl_vars["col"] = new Smarty_Variable; $_smarty_tpl->tpl_vars["col"]->_loop = false;
 $_from = ($_smarty_tpl->tpl_vars['columns']->value); if (!is_array($_from) && !is_object($_from)) { settype($_from, 'array');}
foreach ($_from as $_smarty_tpl->tpl_vars["col"]->key => $_smarty_tpl->tpl_vars["col"]->value){
$_smarty_tpl->tpl_vars["col"]->_loop = true;
?>
            '<?php echo $_smarty_tpl->tpl_vars['col']->value['name'];?>
' => '<?php echo $_smarty_tpl->tpl_vars['col']->value['type'];?>
',
        <?php } ?>
    );
    
    public static $autoCreates = array(
        <?php  $_smarty_tpl->tpl_vars["v"] = new Smarty_Variable; $_smarty_tpl->tpl_vars["v"]->_loop = false;
 $_smarty_tpl->tpl_vars["k"] = new Smarty_Variable;
 $_from = ($_smarty_tpl->tpl_vars['autoCreates']->value); if (!is_array($_from) && !is_object($_from)) { settype($_from, 'array');}
foreach ($_from as $_smarty_tpl->tpl_vars["v"]->key => $_smarty_tpl->tpl_vars["v"]->value){
$_smarty_tpl->tpl_vars["v"]->_loop = true;
 $_smarty_tpl->tpl_vars["k"]->value = $_smarty_tpl->tpl_vars["v"]->key;
?>
            '<?php echo $_smarty_tpl->tpl_vars['k']->value;?>
' => '<?php echo $_smarty_tpl->tpl_vars['v']->value;?>
',
        <?php } ?>
    );
    
    public static $autoUpdates = array(
        <?php  $_smarty_tpl->tpl_vars["v"] = new Smarty_Variable; $_smarty_tpl->tpl_vars["v"]->_loop = false;
 $_smarty_tpl->tpl_vars["k"] = new Smarty_Variable;
 $_from = ($_smarty_tpl->tpl_vars['autoUpdates']->value); if (!is_array($_from) && !is_object($_from)) { settype($_from, 'array');}
foreach ($_from as $_smarty_tpl->tpl_vars["v"]->key => $_smarty_tpl->tpl_vars["v"]->value){
$_smarty_tpl->tpl_vars["v"]->_loop = true;
 $_smarty_tpl->tpl_vars["k"]->value = $_smarty_tpl->tpl_vars["v"]->key;
?>
            '<?php echo $_smarty_tpl->tpl_vars['k']->value;?>
' => '<?php echo $_smarty_tpl->tpl_vars['v']->value;?>
',
        <?php } ?>
    );

    public function findAll() {
        $keys = self::$listfields;
        $sql = "select 
                    <?php echo $_smarty_tpl->tpl_vars['pk']->value;?>
,
                    <?php echo $_smarty_tpl->tpl_vars['caption']->value;?>
,
                    {$keys}
                from <?php echo $_smarty_tpl->tpl_vars['tableName']->value;?>

                limit 10000
            ";

        $sth = Load::db()->prepare($sql);

        $sth->execute();

        return $sth->fetchAll(PDO::FETCH_ASSOC);
    }

    public function find($id) {
        $keys = self::$showfields;

        $sql = "select 
                    <?php echo $_smarty_tpl->tpl_vars['pk']->value;?>
,
                    {$keys} 
                from <?php echo $_smarty_tpl->tpl_vars['tableName']->value;?>

                where <?php echo $_smarty_tpl->tpl_vars['pk']->value;?>
 = ?
                limit 1
            ";

        $sth = Load::db()->prepare($sql);

        $sth->execute(array($id));

        return $sth->fetch(PDO::FETCH_ASSOC);
    }

    public function save($<?php echo $_smarty_tpl->tpl_vars['resourceName']->value;?>
) {
     

        $keys = self::$modifyfields;

        $values = implode(',',array_fill(0, sizeof(explode(',',$keys)), '?'));
        
        $data = array_values(SqlTemplate::filterFields($keys, $<?php echo $_smarty_tpl->tpl_vars['resourceName']->value;?>
));
        
        
        if(sizeof(self::$autoCreates)>0)
        {
            $keys = $keys.','.implode(',',array_keys(self::$autoCreates));
            
            $values = $values.','.implode(',',array_values(self::$autoCreates));
        }

        $sql = "insert into <?php echo $_smarty_tpl->tpl_vars['tableName']->value;?>

                (
                    $keys
                )
                values
                (
                    $values
                )
            ";
       
        $db = Load::db();
        $sth = $db->prepare($sql);

         $result = $sth->execute($data);

 

        if ($result) {
            
            $lastInsertId = Load::db()->lastInsertId();
           
            return $lastInsertId;
        }
        
        return FALSE ;
    }

    public function update($<?php echo $_smarty_tpl->tpl_vars['resourceName']->value;?>
) {

        $t = SqlTemplate::updatePairs(self::$modifyfields, ':');

        
        
        if(sizeof(self::$autoUpdates)>0)
        {
            $arr = array();
            foreach(self::$autoUpdates as $k=>$v)
            {
            
                $arr[] = "{$k}={$v}";
                
             }
           $t = $t.','.implode(',',$arr);      
             
        }
        
        $sql = "update <?php echo $_smarty_tpl->tpl_vars['tableName']->value;?>
 set
                $t
                where <?php echo $_smarty_tpl->tpl_vars['pk']->value;?>
 = :<?php echo $_smarty_tpl->tpl_vars['pk']->value;?>

                limit 1
            ";
              
        $sth = Load::db()->prepare($sql);

        return $sth->execute($<?php echo $_smarty_tpl->tpl_vars['resourceName']->value;?>
);
    }

    public function delete($id) {
        $sql = "delete 
                from <?php echo $_smarty_tpl->tpl_vars['tableName']->value;?>

                where <?php echo $_smarty_tpl->tpl_vars['pk']->value;?>
 = ?
                limit 1
            ";
        $sth = Load::db()->prepare($sql);

        return $sth->execute(array($id));
    }

}

?<?php ?>><?php }} ?>